/*
 * Copyright 2004-2005 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.base.web.view;

import org.apache.log4j.Logger;

public class TreeNode
{
        static Logger logger = Logger.getLogger(TreeNode.class);

        /** id */
        private String id;

        /** type */
        private String type;

        /** 父节点id */
        private String parentId;

        /** 父节点类型 */
        private String parentType;

        /** 节点显示文本 */
        private String text;

        /** 打开的图片 */
        private String openImageFileName;

        /** 关闭的图片 */
        private String closeImageFileName;

        /** 点击用的url */
        private String url;

        /** 根节点 */
        public TreeNode(String id,
                        String type,
                        String ttext,
                        String openImageFileName,
                        String closeImageFileName,
                        String url)
        {
                logger.debug("构造根节点" + type + id);
                this.id = id;
                this.type = type;
                if(ttext == null)
                        this.text = "";
                else
                        this.text = ttext ;
                this.openImageFileName = openImageFileName;
                this.closeImageFileName = closeImageFileName;
                this.url = url;
        }

        /** 非根节点 */
        public TreeNode(String id,
                        String type,
                        String ttext,
                        String parentId,
                        String parentType,
                        String openImageFileName,
                        String closeImageFileName,
                        String url)
        {
                logger.debug("构造非根节点" + type + id);
                this.id = id;
                this.type = type;
                if(ttext == null)
                        this.text = "";
                else
                        this.text = ttext ;
                this.parentId = parentId;
                this.parentType = parentType;
                this.openImageFileName = openImageFileName;
                this.closeImageFileName = closeImageFileName;
                this.url = url;
        }

        public String getId()
        {
                return this.id;
        }

        public String getType()
        {
                return this.type;
        }

        public String getText()
        {
                return this.text;
        }

        public String getParentId()
        {
                return this.parentId;
        }

        public String getParentType()
        {
                return this.parentType;
        }

        public String getOpenImageFileName()
        {
                return this.openImageFileName;
        }

        public String getCloseImageFileName()
        {
                return this.closeImageFileName;
        }

        public String getUrl()
        {
                return this.url;
        }

        // 是否是根节点
        public boolean isRootNode()
        {
                if(this.parentId == null
                && this.parentType == null)
                {
                        return true;
                }
                else
                {
                        return false;
                }
        }

        // 获取js script for xtree
        public String getScript4XTree(String treeImagePath)
        {
                if(this.isRootNode())
                        return this.getRootNodeScript4XTree(treeImagePath);
                else
                        return this.getNodeScript4XTree(treeImagePath);
        }

        // 取得根节点script
        private String getRootNodeScript4XTree(String treeImagePath)
        {
                logger.debug("获取根结点script4xtree :" + this.getNodeId4XTree());
                StringBuffer treeNode=new StringBuffer(100);
                treeNode.append("var ");
                treeNode.append(this.getNodeId4XTree());
                treeNode.append(" = new WebFXTree('<img src=\"");
                treeNode.append(treeImagePath);
                treeNode.append(this.openImageFileName);
                treeNode.append("\">");
                treeNode.append(this.text);
                treeNode.append("','");
                treeNode.append(this.url);
                treeNode.append("');");
                treeNode.append(this.getNodeId4XTree());
	        treeNode.append(".setBehavior('classic');");
                return treeNode.toString();
        }

        // 取得非根节点script
        private String getNodeScript4XTree(String treeImagePath)
        {
                logger.debug("获取非根结点script4xtree :" + this.getNodeId4XTree());
                StringBuffer script = new StringBuffer(100);
                script.append("var ");
                script.append(this.getNodeId4XTree());
                // 获取当前节点id
                script.append(" = new ");
                script.append("WebFXTreeItem");
                script.append("(\"");
                // 获取当前节点名称
                script.append(this.text);
                script.append("\", ");
                script.append("\"");
                // 节点点击触发事件
                script.append(this.url);
                script.append("\",");
                // 父节点id
                script.append(this.getParentNodeId4XTree());
                script.append(",\"");
                // 展开前图片
                script.append(treeImagePath);
                script.append(this.closeImageFileName);
                script.append("\"" + ",\"");
                // 展开后图片
                script.append(treeImagePath);
                script.append(this.openImageFileName);
                script.append("\");");
                return script.toString();
        }

        public String getNodeId4XTree()
        {
                return this.type + this.id;
        }

        public String getParentNodeId4XTree()
        {
                return this.parentType + this.parentId;
        }

        public boolean equals(Object object)
	{
		if (!(object instanceof TreeNode))
		{
			return false;
		}
		TreeNode treeNode = (TreeNode)object;
		return this.getNodeId4XTree().equals(treeNode.getNodeId4XTree());
	}

	public int hashCode()
	{
		return this.getNodeId4XTree().hashCode();
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer(100);
		sb.append(this.getClass());
		sb.append(" < ");
		sb.append("id: ");
		sb.append(this.getNodeId4XTree());
		sb.append(", text: ");
		sb.append(this.text);
		sb.append(" > ");
		return sb.toString();
	}
}

